package com.kyzh.core.pager.weal.community

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import com.gushenge.core.beans.CommunityListBean
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.pager.weal.community.adapter.CommunityListAdapter
import com.kyzh.core.pager.weal.community.viewmodel.CommunityViewModel
import com.kyzh.core.databinding.ActCommonitylistBinding
import org.jetbrains.anko.support.v4.onRefresh
import androidx.core.util.Pair
import com.kingja.loadsir.core.LoadService
import com.kyzh.core.utils.loadServiceInit
import com.kyzh.core.utils.setVisibility
import com.kyzh.core.utils.showLoading

class CommunityListActivity: BaseActivity() {
    private val adapter = CommunityListAdapter()
    private val vm by viewModels<CommunityViewModel>()
    private lateinit var db : ActCommonitylistBinding
    private var p = 1
    private var keyword = ""
    private val type = 0
    private lateinit var loadsir: LoadService<Any>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db = DataBindingUtil.setContentView(this, R.layout.act_commonitylist)
        db.rev.adapter = adapter
        loadsir = loadServiceInit(db.root) {
            //点击重试时触发的操作
            loadsir.showLoading()
        }
        initView()
        getDate()
        initSearch()
    }
    fun initView(){
        db.searchClose.setVisibility(true)
        db.searchClose.setOnClickListener {
            finish()
        }
        db.tvSearch.setOnClickListener {
            val words =db. edittext.text.toString().trim()
            p= 1
            vm.getData(p,type,words){
                adapter.setNewInstance(this.data)
            }
        }
        db.swipe.onRefresh {
            p = 1
            getDate()
        }
        adapter.loadMoreModule.setOnLoadMoreListener {
            p ++
            vm.getData(p,type){
                adapter.addData(this.data)
                adapter.loadMoreModule.loadMoreComplete()
                if(p >= maxP){
                    adapter.loadMoreModule.loadMoreEnd(true)
                }
            }
        }
        adapter.setOnItemClickListener{a,d,c->
            val data = a.data[c] as CommunityListBean.Data
            val pair = Pair<View, String>(d.findViewById(R.id.icon), "icon")
            val pair1 = Pair<View, String>(d.findViewById(R.id.tvName), "tvName")
            val pair2= Pair<View, String>(d.findViewById(R.id.ivGuanzhu), "ivGuanzhu")
            CommunityActivity.start(this,data.id,pair,pair1,pair2)
        }
        adapter.setOnItemChildClickListener{a,b,c->
            when(b.id){
                R.id.ivGuanzhu ->{
                    val data = a.data[c] as CommunityListBean.Data
                    vm.guanzhu(data.id) {
                        data.isGuanzhu = !data.isGuanzhu
                        adapter.notifyItemChanged(c)
                    }
                }
            }
        }

    }
    fun initSearch(){
        db.edittext.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                /*隐藏软键盘*/
                val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                if (imm.isActive) imm.hideSoftInputFromWindow(v.applicationWindowToken, 0)
                val words =db. edittext.text.toString().trim()
                p = 1
                if (words.isNotEmpty()) {
                    vm.getData(p,type,words){
                        db.swipe.isRefreshing = false
                        adapter.setNewInstance(this.data)
                    }
                }else{
                    vm.getData(p,type){
                        adapter.setNewInstance(this.data)
                    }
                }
            }
            true
        }
    }


    fun getDate(){
        vm.getData(p,type){
            db.swipe.isRefreshing = false
            loadsir.showSuccess()
            adapter.setNewInstance(this.data)
        }
    }
}